//
//  QDMetroCountTableViewCell.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/2.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 购买数量的集合cell
final class QDMetroCountCollectionView: NWCollectionViewCell {
    
    /// 设置是否选中状态,默认是选不中的
    var select: Bool  = false {
        willSet {
            if newValue {
                titleLabel.backgroundColor = QDColors.theme
                titleLabel.textColor = .white
                titleLabel.layer.borderWidth = 0
            }else {
                titleLabel.backgroundColor = .white
                titleLabel.textColor = QDColors.gray
                titleLabel.layer.borderWidth = 0.5
            }
        }
    }
    
    /// 显示张数的标签
    let titleLabel = UILabel()
    
    override func buildView() {
        super.buildView()
        
        titleLabel.text = "9张"
        titleLabel.layer.cornerRadius = 4
        titleLabel.layer.borderColor = #colorLiteral(red: 0.8901960784, green: 0.9019607843, blue: 0.8941176471, alpha: 1).cgColor
        titleLabel.clipsToBounds = true
        titleLabel.textAlignment = .center
        
        contentView.addSubview(titleLabel)

        titleLabel.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}

protocol QDMetroCountChangeActor: class {
    
    /// 当前点击数获得变化
    func metroCountTableViewCell(cell: QDMetroCountTableViewCell, currentIndex: NSInteger)
}

extension QDMetroCountChangeActor {
    
    func metroCountTableViewCell(cell: QDMetroCountTableViewCell, currentIndex: NSInteger){}
}

/// 购买数量
final class QDMetroCountTableViewCell: NWTableViewCell {
    
    /// 比如大于0
    static func height(count: NSInteger) -> CGFloat {
        
        let top = 15 + 22 + 10 + 10 //title.top、title的高度、边距高度、collectionView.bottom
        guard count > 0 else { return CGFloat(top) }
        
        let row = count % 4 == 0 ? count / 4 : (count / 4 + 1)//计算行数
        return CGFloat(top + row * 36 + 10 * (row - 1))
    }

    /// 默认选中第一个
    private var currentIndex = 0 {
        willSet {
            countActor?.metroCountTableViewCell(cell: self, currentIndex: newValue)
        }
    }
    /// 集合视图
    lazy private var collectionView: UICollectionView = {
       return UICollectionView(frame: .zero, collectionViewLayout: self.flowLayout)
    }()
    
    /// 布局对象
    lazy private var flowLayout: UICollectionViewFlowLayout = {
       
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: 70, height: 36)
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        flowLayout.scrollDirection = .vertical
        
        // 间距控制器
        flowLayout.minimumLineSpacing = 10
        flowLayout.minimumInteritemSpacing = (UIScreen.main.ritl_width - 40 - 70 * 4) / 3
        
        return flowLayout
    }()
    
    /// 显示购买数量的
    let titleLabel = UILabel()
    /// 数量变化响应对象
    weak var countActor: QDMetroCountChangeActor?
    /// 个数
    var count = 1 {
        didSet {
            if oldValue != count {
                collectionView.reloadData()
            }
        }
    }
    
    override func buildView() {
        super.buildView()
        
        titleLabel.text = "购买数量"
        titleLabel.textColor = 179.ritl_color
        titleLabel.font = Font.regular.font(size: 16)
        
        collectionView.backgroundColor = .white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(QDMetroCountCollectionView.self, forCellWithReuseIdentifier: "cell")
        
        bottomLineView.backgroundColor = #colorLiteral(red: 0.9294117647, green: 0.9411764706, blue: 0.9333333333, alpha: 1)
        bottomLineInset.left = 20
        bottomLineInset.right = 20
        bottomLineHeight = 0.5

        contentView.addSubview(titleLabel)
        contentView.addSubview(collectionView)
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(15)
            make.height.equalTo(22)
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.right.left.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.bottom.equalToSuperview().inset(10)
        }
    }
}

extension QDMetroCountTableViewCell: UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        updateSelectStatus(isSelect: indexPath.item == currentIndex, index: indexPath.item, cell: cell)
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        //如果重复，则不进行回调
        if indexPath.item == currentIndex { return }
        //修复头一个状态
        updateSelectStatus(isSelect: false, index: currentIndex, cell: collectionView.cellForItem(at: IndexPath(item: currentIndex, section: 0)))
        //
        currentIndex = indexPath.item
        //更新现有
        updateSelectStatus(isSelect: true, index: currentIndex, cell: collectionView.cellForItem(at: IndexPath(item: currentIndex, section: 0)))
    }
    
    
    /// 修改选中状态
    private func updateSelectStatus(isSelect: Bool, index: NSInteger,cell: UICollectionViewCell?) {
        
        if let countCell = cell as? QDMetroCountCollectionView {
            countCell.select = isSelect
            countCell.titleLabel.text = "\(index + 1)张"
        }
    }
}
